package com.game.raceroom.player	
{
	import com.util.Vector;
	import com.game.raceroom.player.contrl.ActorContrl;
	
	public class Action
	{

		
		private static var LEFT: int=0x00000001;
		private static var RIGHT:int=0x00000002;
		private static var UP:   int=0x00000004;
		private static var DOWN: int=0x00000008;
		
		private static var RELASE_ACC: int=0x00000010;
		private static var BRAKE:      int=0x00000020;
		private static var BRAKE_UP:   int=0x00000040;
		private static var BRAKE_DOWN: int=0x00000080;
		
		private static var KEY_PRESS:   int=0x00010000;
		private static var KEY_RELEASE: int=0x00020000;
		
		private var actor:Player;
		private var acode:int;
		public function Action(actor:Player)
		{
			this.actor=actor;
		}
		//分解动作代码
        // int action 0000 0000 0000 0000 0000 0000 0000 0000
        // 0-3位 上下左右标志
        // 4-7位 松开油门 刹车 倒退前进 前进倒退标志
        // 8-15 按键代码
        // 16 按键标志
        // 17 释放键标志
		public function parseActionCode(acode:int):void{
			if((acode&LEFT)>0)
				actor._left=true;
			else
				actor._left=false;
				
			if((acode&RIGHT)>0)
				actor._right=true;
		    else
		        actor._right=false;
		        
			if((acode&UP)>0)
				actor._up=true;
			else
			    actor._up=false;
			    
			if((acode&DOWN)>0)
				actor._down=true;
			else
				actor._down=false;
				
			if((acode&RELASE_ACC)>0)
				actor._relaseAccelerator=true;
			else 
				actor._relaseAccelerator=false;
				
			if((acode&BRAKE)>0)
				actor._brake=true;
			else 
				actor._brake=false;
				
			if((acode&BRAKE_UP)>0)
				actor._brake_up=true;
			else 
			 	actor._brake_up=false;
			 	
			if((acode&BRAKE_DOWN)>0)
				actor._brake_down=true;
			else 
		  		actor._brake_down=false;	
 
			
		}
		//获得动作代码
		public function getKeyCode(press:Boolean,release:Boolean,code:int):int{
			acode=0;
			if(press)
				acode=acode|KEY_PRESS;
			if(release)
				acode=acode|KEY_RELEASE;
				
			acode=acode|code;
			return acode;
		}	
		public function setKeyCode(acode:int):void{
			if((acode&KEY_PRESS)>0)
				((ActorContrl)(actor)).keyPress(acode&0x0000FF00);
				
			if((acode&KEY_RELEASE)>0)
				((ActorContrl)(actor)).keyRelease(acode&0x0000FF00);
		}
		public function getCurentActionCode():int{
			acode=0;
			if(actor._left)
				acode=acode|LEFT;
			if(actor._right)
				acode=acode|RIGHT;
			if(actor._up)
				acode=acode|UP;
			if(actor._down)
				acode=acode|DOWN;
			if(actor._relaseAccelerator)
				acode=acode|RELASE_ACC;
			if(actor._brake)
				acode=acode|BRAKE;
			if(actor._brake_up)
				acode=acode|BRAKE_UP;
			if(actor._brake_down)
				acode=acode|BRAKE_DOWN;
			return acode;
		}
		private var key:int=0;
		private var key1:int=0;
		private var ac:int=0;
		private var ac1:int=0;
		private var act:int=0;
		public function composeKeyCode(actions:Vector,a:int):void{
			if(actions.size()==0){
				actions.put(a);
				//trace(a);
			}else{
				key1=a&0x0000FF00;
				ac1=a&0x000F0000;
				for(var i:int=0;i<actions.size();i++){
					act=(int)(actions.getObjById(i));
					key=act&0x0000FF00;
					ac=act&0x000F0000;
					if(key==key1){
						if(ac1==ac){
							 return;
						}else{
							 actions.remove(i);
							 return;
						}
					}
				}
				actions.put(a);
				//trace(a);
			}

		}
	}
}